/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 活跃奖励模块
	/// module : activity
	/// </summary>
	public interface SNetActivity : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.activity_sign, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.activity_sign, "Recv->" + data.ToString());
#endif
		                SignRequest _data = (SignRequest)data;
						return CS_sign(session);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.activity_sign, ()=> new SignRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        ///  签到
        /// </summary>
	    protected SignResult CS_sign(ISession session);
#endregion
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
        public SignResult SC_signResult(Task<SignResult> future){return new SignResult(future);}
        public SignResult SC_signResult(int code, int signInfo, List<ItemShowPOD> shows){return new SignResult(code, signInfo, shows);}
        public SignResult SC_signResult(int code){return new SignResult(code);}
		/// <summary>
        ///  推送签到
        /// </summary>
		public bool SC_notifySign(ISession session, int signInfo){
	        return NetProtocolHelper.SendMessage(session, new NotifySign(signInfo));
	    }
	    public void SC_notifySign_Broadcast(List<ISession> sessions, int signInfo){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifySign(signInfo));
	    }
#endregion

#region Messages
	public class SignResult : MessageResult {
	    private int signInfo;
	    private List<ItemShowPOD> shows;
	    public SignResult(Task<SignResult> future): base(future) { }
		public SignResult(int code): base(code) { }
		public new SignResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal SignResult(int code, int signInfo, List<ItemShowPOD> shows){
	        this.code = code;
	        this.signInfo = signInfo;
	        this.shows = shows == null ? null : new (shows);
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<SignResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.activity_signResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.activity_signResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_int(signInfo);
			_out.Write_list_pod(shows);
			return si_p;
		}
		public override string ToString() {
    		return "Activity.SC_signResult { "
			+ "code="
			+ code
			+ ", signInfo="
			+ signInfo
			+ ", shows="
			+ shows.ToString<ItemShowPOD>()
			+" }";
		}
	}
	public class NotifySign : MessageNotify {
		private int signInfo;
		public NotifySign(int signInfo) {
		    this.signInfo = signInfo;
		}
		public override int GetMessageId() {return NetMessageId.activity_notifySign;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.activity_notifySign;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(signInfo);
			return si_p;
		}
		public override string ToString() {
			return "Activity.SC_notifySign { "
			+ "signInfo="
			+ signInfo
			+" }";
		}
	}
	private class SignRequest : IReceiveMessage
    {
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
		}
        public int GetMessageId() { return NetMessageId.activity_sign; }
        public override string ToString()
        {
            return "Activity.sign";
        }
    }
	}
#endregion
}